<?php
/**
 * 报告许可（权限）
 * (model)
 */
namespace App\Util;

use App\Models\Basics\SysArea;
use Encore\Admin\Facades\Admin;
use Illuminate\Database\Eloquent\Builder;
use Encore\Admin\Grid;
class ReportPermission
{
    public static function classifyRClassify(Builder $query){
        if(!Admin::user()->isRole('administrator') && Admin::user()->can('sales-report.payment.classify')){
            $query->whereHas('classify', function ($query){
                $query->where('report_status', 1);
            });
        }
        return $query;
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public static function area(Builder $query){
        if(Admin::user()->inRoles(['sales-executive','salesperson'])){
            if(Admin::user()->inRoles(['eu-sales-executive','eu-salesperson'])){
                $query->whereHas('area', function ($query){
                    $query->whereIn('area', SysArea::$_EUArea);
                });
            }elseif(Admin::user()->inRoles(['us-sales-executive','us-salesperson'])){
                $query->whereHas('area', function ($query){
                    $query->whereIn('area', SysArea::$_NAArea);
                });
            }elseif(Admin::user()->inRoles(['jp-sales-executive','jp-salesperson'])){
                $query->whereHas('area', function ($query){
                    $query->whereIn('area', SysArea::$_JPArea);
                });
            }
        }
        return $query;
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public static function country(Builder $query){
        if(Admin::user()->inRoles(['sales-executive','salesperson'])){
            if(Admin::user()->inRoles(['eu-sales-executive','eu-salesperson'])){
                $query->whereHas('country', function ($query){
                    $query->whereIn('country', SysArea::$_EUCountry);
                });
            }elseif(Admin::user()->inRoles(['us-sales-executive','us-salesperson'])){
                $query->whereHas('country', function ($query){
                    $query->whereIn('country', SysArea::$_NACountry);
                });
            }elseif(Admin::user()->inRoles(['jp-sales-executive','jp-salesperson'])){
                $query->whereHas('country', function ($query){
                    $query->whereIn('country', SysArea::$_JPCountry);
                });
            }
        }
        return $query;
    }

    /**
     * @param Grid $grid
     * @return Grid
     */
    public static function gridCountry(Grid $grid){
        if(Admin::user()->inRoles(['sales-executive','salesperson'])){
            if(Admin::user()->inRoles(['eu-sales-executive','eu-salesperson'])){
                $grid->model()->whereHas('country', function ($query){
                    $query->whereIn('country', SysArea::$_EUCountry);
                });
            }elseif(Admin::user()->inRoles(['us-sales-executive','us-salesperson'])){
                $grid->model()->whereHas('country', function ($query){
                    $query->whereIn('country', SysArea::$_NACountry);
                });
            }elseif(Admin::user()->inRoles(['jp-sales-executive','jp-salesperson'])){
                $grid->model()->whereHas('country', function ($query){
                    $query->whereIn('country', SysArea::$_JPCountry);
                });
            }
        }
        return $grid;
    }

    /**
     * @param Builder $query
     * @return Builder
     */
    public static function seller(Builder $query){
        if(Admin::user()->inRoles(['sales-executive','salesperson'])){
            if(Admin::user()->inRoles(['eu-sales-executive','eu-salesperson'])){
                $sellerId = User::getASellerIds('eu');
                $query->whereIn('seller_id', $sellerId);
            }elseif(Admin::user()->inRoles(['us-sales-executive','us-salesperson'])){
                $sellerId = User::getASellerIds('us');
                $query->whereIn('seller_id', $sellerId);
            }elseif(Admin::user()->inRoles(['jp-sales-executive','jp-salesperson'])){
                $sellerId = User::getASellerIds('jp');
                $query->whereIn('seller_id', $sellerId);
            }
        }
        return $query;
    }
}
